package com.education.module.message;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.LoginUser;
import com.education.domain.Message;
import com.education.framework.base.BaseServices;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;

@Service
public class MessageServices extends BaseServices implements IDao<Message>{

	@Override
	public List<Message> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		StringBuffer sqlWhere = new StringBuffer();
		sql.append("SELECT id,title,msg,msgType,update_time FROM message");
		String lp = " where ";
		List<Object> argsList = new ArrayList<Object>();
		if(null != searchParams){
			if(null != searchParams.get("title") && !"".equals((String)searchParams.get("title"))){
				sqlWhere.append(lp).append(" title like ? ");
				argsList.add("%" + searchParams.get("title") + "%");
				lp = " and ";
			}
		}
		Object[] args = argsList.toArray();
		sql.append(sqlWhere.toString());
		sql.append(" order by update_time desc");
		List<Message> list = dao.query(pageSQL(sql.toString(),page),args,new MessageRowmapper());
		page.setTotalItem(findRecordCount("select count(id) as cNum from message " + sqlWhere.toString(),args));
		return list;
	}
	
	public List<Message> find() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,title,msg,msgType,update_time FROM message order by update_time desc");
		
		List<Message> list = dao.query(sql.toString(),new MessageRowmapper());
		return list;
	}
	public List<Message> find(String msgType) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,title,msg,msgType,update_time FROM message");
		if(null != msgType && !"".equals(msgType)){
			sql.append(" where msgType='").append(msgType).append("'");
		}
		
		List<Message> list = dao.query(sql.toString(),new MessageRowmapper());
		return list;
	}
	
	public int findCount(String id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT count(id) FROM message where id=?");
		int c = dao.queryForInt(sql.toString(),new Object[]{id});
		return c;
	}

	@Override
	public int save(Message obj) {
		 
		 StringBuffer sql = new StringBuffer(); 
		 sql.append("insert into message ( "); 
		 sql.append("id,title,msg,msgType ");  
		 sql.append(" ) values(?,?,?,?) "); 
		 Object[] args = {obj.getId(),obj.getTitle(),obj.getMsg(),obj.getMsgType() };
		 
		 dao.update(sql.toString(), args);
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public Message findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,title,msg,msgType,update_time FROM message ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new MessageRowmapper());
	}

	@Override
	public void update(Message obj) {
		 
		 StringBuffer sql = new StringBuffer(); 
		 sql.append("update message "); 
		 sql.append("set  "); 
		 sql.append("title=?,msg=?,msgType=? where id=?  "); 
		 Object[] args = {obj.getTitle(),obj.getMsg(),obj.getMsgType(),obj.getId() };
		 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from message where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class MessageRowmapper implements RowMapper<Message> {
		@Override
		public Message mapRow(ResultSet rs, int arg1) throws SQLException {
			Message obj = new Message();
			obj.setId(rs.getInt("id")); 
			obj.setTitle(rs.getString("title")); 
			obj.setUpdateTime(rs.getString("update_time")); 
			obj.setMsgType(rs.getInt("msgType")); 
			obj.setMsg(rs.getString("msg")); 

			return obj;
		}
	}

	public boolean checkLogin(LoginUser loginUser) {
		if(null == loginUser) return false;
		if("".equals(loginUser.getLoginName()) || "".equals(loginUser.getPasswd())) return false;
		boolean r = false;
		List<Map<String,Object>> list = null;
		if("1".equals(loginUser.getType())){
			StringBuffer sqlTeacher = new StringBuffer();
			sqlTeacher.append("select id as cc from teacher where loginname=? and passwd=?");
			Object[] args = {loginUser.getLoginName(),loginUser.getPasswd()};
			list = dao.queryForList(sqlTeacher.toString(),args);
		}else{
			StringBuffer sqlStudent = new StringBuffer();
			sqlStudent.append("select id as cc from student where loginname=? and passwd=?");
			Object[] args = {loginUser.getLoginName(),loginUser.getPasswd()};
			list = dao.queryForList(sqlStudent.toString(),args);
		}
		
		if(list.size() > 0){
			Map<String,Object> m = list.get(0);
			int id = (Integer)m.get("cc");
			loginUser.setId(id);
			
			r = true; //验证成功
		}
		return r;
	}

}
